package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.User;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/v1/user")
@Slf4j
public class UserController {

    static final ReentrantLock lock=new ReentrantLock();

    @Autowired
    UserService userService;

    @PostMapping("/save_or_update")
    public  void  login(@RequestBody User user) {

        lock.lock();
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getName, user.getName());
            User one = userService.getOne(wrapper);
            if (one != null) {
                user.setId(one.getId());
                log.info("+++++++++++++++++++");
            } else {
                log.info("-----------------------");
            }

//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

            log.info("user:{}", user);
            userService.saveOrUpdate(user);
            log.info("insert success");
            lock.unlock();
    }
}
